// @Auth:zdl
package record

import (
	"sync"
	"time"

	"github.com/gogf/gf/g/os/gcron"

	"github.com/zhongdalu/DoorValveErWang/model"
	"github.com/zhongdalu/DoorValveErWang/util"
)

type BackMsg struct {
	Xyh        int         `json:"xyh"`
	Dt         string      `json:"dt"`
	Fac        string      `json:"fac"`
	OrderID    int         `json:"order_id"`
	Dtu        string      `json:"dtu"`
	Cjq        string      `json:"cjq"`
	Socket     string      `json:"socket"`
	CmdID      string      `json:"cmd_id"`
	Data       interface{} `json:"data"`
	Errno      int         `json:"errno"`
	Error      string      `json:"error"`
	IsComplete bool        `json:"is_complete"`
}

type SplitMsg struct {
	Xyh   int      `json:"xyh"`
	UUID  string   `json:"uuid"`
	Dt    string   `json:"dt"`
	CmdID []string `json:"cmd_id"`
	Errno int      `json:"errno"`
	Error string   `json:"error"`
}

type CmdRecord struct {
	mu              sync.RWMutex
	step            int
	balanceInterval int
	checkInterval   int
	beginTs         int64 // 开始时间戳
	biEntry         *gcron.Entry
	ciEntry         *gcron.Entry
	recordEntry     *gcron.Entry
	allNum          int
	classId         string
}

func NewCmdRecord() *CmdRecord {
	return &CmdRecord{
		mu:      sync.RWMutex{},
		beginTs: time.Now().Local().Unix(),
	}
}

func (r *CmdRecord) SetAllNum(num int) {
	r.allNum = num
}

func (r *CmdRecord) RegisterRecordTimer(entry *gcron.Entry) {
	r.recordEntry = entry
}

func (r *CmdRecord) CancelRecordTimer() {
	if r.recordEntry != nil {
		r.recordEntry.Close()
		r.recordEntry = nil
	}
}

func (r *CmdRecord) RegisterBiTimer(entry *gcron.Entry) {
	r.biEntry = entry
}

func (r *CmdRecord) CancelBiTimer() {
	if r.biEntry != nil {
		r.biEntry.Close()
		r.biEntry = nil
	}
}

func (r *CmdRecord) RegisterCiTimer(entry *gcron.Entry) {
	r.ciEntry = entry
}

func (r *CmdRecord) CancelCiTimer() {
	if r.ciEntry != nil {
		r.ciEntry.Close()
		r.ciEntry = nil
	}
}

func (r *CmdRecord) GetAllNum() int {
	return r.allNum
}

func (r *CmdRecord) GetBeginTs() int64 {
	return r.beginTs
}

func (r *CmdRecord) SetBalanceInterval(i int) {
	r.mu.RLock()
	defer r.mu.RUnlock()
	r.balanceInterval = i
}

func (r *CmdRecord) GetBalanceInterval() int {
	r.mu.RLock()
	defer r.mu.RUnlock()
	return r.balanceInterval
}

func (r *CmdRecord) SetCheckInterval(i int) {
	r.mu.RLock()
	defer r.mu.RUnlock()
	r.checkInterval = i
}

func (r *CmdRecord) SetStep(i int) {
	r.mu.RLock()
	defer r.mu.RUnlock()
	r.step = i
}

func (r *CmdRecord) GetCheckInterval() int {
	r.mu.RLock()
	defer r.mu.RUnlock()
	return r.checkInterval
}

func (r *CmdRecord) GetStep() int {
	r.mu.RLock()
	defer r.mu.RUnlock()
	return r.step
}

func (r *CmdRecord) GenClassId() {
	r.mu.RLock()
	defer r.mu.RUnlock()
	r.classId = util.GenUuid()
}

func (r *CmdRecord) GetClassId() string {
	r.mu.RLock()
	defer r.mu.RUnlock()
	return r.classId
}

func (r *CmdRecord) CancelAllTimer() {
	r.CancelCiTimer()
	r.CancelBiTimer()
	r.CancelRecordTimer()
}

func (r *CmdRecord) Reset() {
	r.beginTs = 0
	r.SetStep(0)
	r.CancelAllTimer()
}

type recordInfo struct {
	Uuid  string
	CmdId string
	model.AddInfo
}

func NewRecordInfo(uuid string, mo model.AddInfo) *recordInfo {
	return &recordInfo{
		Uuid:    uuid,
		AddInfo: mo,
	}
}
